package statistics;

import java.util.Random;

/**
 * This class represents an Exponential Distribution.
 */

public class ExponentialDistribution extends ContinuousDistribution {

  private double lambda;

  /**
   * Constructs an Exponential Distribution with parameter lambda. You must also
   * specify the random seed that will be used to realize random variables with this
   * distribution.
   * @param lambda the parameter of the Exponential Distribution with expectation 1/lambda.
   * @param random the randomseed.
   */

  public ExponentialDistribution(double lambda,Random random) {
    this.lambda = lambda;
    this.random = random;
  }


  /**
   * Returns the expectation of an Exponential distribution with parameter lambda.
   */

  public double expectation() {
    return 1.0/lambda;
  }

  /**
   * Returns the variance of an Exponential distribution with parameter lambda.
   */

  public double variance() {
    return 1.0/(lambda*lambda);
  }

  /**
   * Returns P(X <= k), X is a random variable with an Exponential distribution.
   * @param k
   */

  public double probabilityFunction(double k) {
    if (k <= 0) return 0;
      else return 1-Math.exp(-lambda*k);
  }

  /**
   * Returns a realization of a random variable with an Exponential distribution.
   */

  public double nextRandom() {
    double u = random.nextDouble();
    return -Math.log(u)/lambda;
  }
}
